package com.nic.view.activity;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import com.nic.view.R;
import com.nic.view.widget.TakePhotoView;

import java.io.Serializable;
import java.util.List;


public class TakePhotoActivity extends VBaseActivity {


    private TakePhotoView takePhoto;
    public static final int RESULT_CODE = 32423;
    public static final String TAKE_PHOTO = "take_photo";
    public static final String TAKE_TAG = "Tag";
    public static final String TAKE_LIST = "list";
    public static final String TAKE_PHOTO_ACTION = "com.take.photo";
    private int tag;

    @Override
    public int getContentLayoutId() {
        return R.layout.activity_take_photo;
    }

    @Override
    public void initView() {
        setToolbarTitle("图片采集");
        getToolbarRightTv().setVisibility(View.VISIBLE);
        getToolbarRightTv().setText("确认");
        getToolbarRightTv().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!takePhoto.getPaths().isEmpty()) {
                    Intent acIntent = new Intent(TAKE_PHOTO_ACTION);
                    acIntent.putExtra(TAKE_PHOTO, (Serializable) takePhoto.getPaths());
                    acIntent.putExtra(TAKE_TAG, tag);
                    sendBroadcast(acIntent);
                    Intent intent = getIntent();
                    intent.putExtra(TAKE_PHOTO, (Serializable) takePhoto.getPaths());
                    setResult(RESULT_CODE, intent);
                }
                finish();
            }
        });
        takePhoto = (TakePhotoView) findViewById(R.id.take_photo);
//        takePhoto.setNoneAdd();
//        takePhoto.setPreview(true);
        tag = getIntent().getIntExtra(TAKE_TAG, 0);
        List<String> list = (List<String>) getIntent().getSerializableExtra(TAKE_LIST);
        if (list != null && !list.isEmpty()) {
            takePhoto.setPreviewUrls(TextUtils.join(",", list), false);
        }
//        PhotoByLocActivity.jumpTo(this, 678);
//        FrameLayout ftest = (FrameLayout) findViewById(R.id.test);
//        presenterControl = new PresenterControl(this, PresenterControl.PresenterType.photo);
//        ftest.addView(presenterControl.getPhotoView());

    }

//    PresenterControl presenterControl;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        takePhoto.onActivityResult(requestCode, resultCode, data);
//        if (requestCode == 678) {
//            List<String> list = (List<String>) data.getSerializableExtra(PhotoByLocActivity.TAKE_PHOTO);
//            takePhoto.setPreviewData(list, true);
//        }
//        presenterControl.setPhotoIdsToList(TextUtils.join(",", takePhoto.getPaths()));
    }

    public static void jumpTo(Activity activity, String tag, List<String> list, int requestCode) {
        Intent intent = new Intent(activity, TakePhotoActivity.class);
        intent.putExtra(TAKE_TAG, tag);
        intent.putExtra(TAKE_LIST, (Serializable) list);
        activity.startActivityForResult(intent, requestCode);
    }

    public static void jumpTo(Activity activity, int requestCode) {
        Intent intent = new Intent(activity, TakePhotoActivity.class);
        activity.startActivityForResult(intent, requestCode);
    }
}
